package com.aptemo.webetek.server.handlers;

import com.aptemo.webetek.client.commands.Action;
import com.aptemo.webetek.client.commands.impl.ModifyTeamStatusAction;
import com.aptemo.webetek.client.enums.EnumTeamStatus;
import com.aptemo.webetek.client.exceptions.BackendException;
import com.aptemo.webetek.client.exceptions.WebetekLoginException;
import com.aptemo.webetek.server.datastore.DataStoreAPI;
import com.aptemo.webetek.server.datastore.DataStoreHelper;
import com.aptemo.webetek.shared.TeamBean;

public class ModifyTeamStatusHandler
		extends ActionHandler<ModifyTeamStatusAction, TeamBean>
{
	private DataStoreHelper	helper	= new DataStoreHelper();

	public ModifyTeamStatusHandler(DataStoreAPI service)
	{
		super(service);
	}

	public Class<? extends Action<?>> getActionType()
	{
		return ModifyTeamStatusAction.class;
	}

	@Override
	public TeamBean invoke(ModifyTeamStatusAction action) throws BackendException, WebetekLoginException
	{
		utils.checkLogin();
		utils.checkAdmin();

		TeamBean team = helper.setTeamStatus(action.getTeamId(), action.getStatus());

		if (action.getStatus() == EnumTeamStatus.ACTIVE && team.getStatus() == EnumTeamStatus.WAITINGFORAPPROVAL)
		{
			String subject = "[Webetek] Team '" + team.getName() + "' created...";
			String content = "Hello " + team.getOwnerEmail() + "!\n";

			content += "\nYour request has been approved! You're the owner/editor of a new Webetek Team!";
			content += "\nTeam title: " + team.getName();
			content += "\nTeam description: " + team.getDescription();

			String path = this.getServletUrl();
			String link = path + "#group-" + team.getId();

			content += "\n";
			content += "\nGo ahead and enjoy the betting party!";
			content += "\n" + link;
			content += "\n";
			content += "\nSincerely yours,";
			content += "\nThe Webetek team";

			// webetek/commandService?actionId=com.aptemo.webetek.client.commands.impl.ModifyTeamStatusAction&json={teamId:"agd3ZWJldGVrchALEglVc2VyR3JvdXAYwwUM",status:"ACTIVE"}
			createEmailSenderTask("", team.getOwnerEmail(), "", subject, content);
		}

		return team;
	}

}
